package com.hu.zhcc.shiro.controller.lgx;

import com.hu.zhcc.shiro.entity.lgx.*;
import com.hu.zhcc.shiro.entity.xy.pojo.TAssess;
import com.hu.zhcc.shiro.service.lgx.*;
import com.hu.zhcc.shiro.service.xy.TAssessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.StringReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class TorderController {
    @Autowired
    TOrderService tOrderService;
    @Autowired
    TOrdersettingService tOrdersettingService;
    @Autowired
    private TAssessService tAssessService;
    @Autowired
    private TMemberExaminationService tMemberExaminationService;
    @Autowired
    private TmemberInfoService tmemberInfoService;
    @Autowired
    TBodyCkService tBodyCkService;
    @Autowired
    TMemberDldateService tMemberDldateService;
    @Autowired
    TMemberRegisterService tMemberRegisterService;
    @Autowired
    TCheckitemService tCheckitemService;
    //预约列表显示
    @GetMapping("/tOrderid")
    public List<TOrder> tOrderid(){
        List<TOrder> tOrders = tOrderService.selectByList();
        return tOrders;
    }
    //预约列表状态修改
    @PostMapping("/tOrderupdate")
    public int tOrderupdate(@RequestBody TOrder tOrder) throws ParseException {
        System.out.println(tOrder);
        int i=0;
        Date date=new Date();
        if(tOrder.getOrderstatus().equals("已到诊")){
          i=0;
        }else {
            if(tOrder.getOrderstatus().equals("未到诊")){
                TMemberExamination tMemberExamination = tMemberExaminationService.tmemberExid(tOrder.getMemberId());
                System.out.println(tMemberExamination);
                //风险评估表
                TAssess tAssess = new TAssess();
                tAssess.setMemberId(tOrder.getMemberId());
                tAssess.setExaminationId(tMemberExaminationService.tmemberExid(tOrder.getMemberId()).getId());
                TMemberInfo tMemberInfo = tmemberInfoService.selectByMemberId(tOrder.getMemberId());
                tAssess.setOperator(tMemberInfo.getHealthMgr().getHealthMgrName());
                tAssess.setState(0);
                String format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
                tAssess.setTime(format);
                System.out.println(tAssess);
                int i2 = tAssessService.insertSelective(tAssess);
                //预约表
                tOrder.setOrderstatus("已到诊");
                i = tOrderService.updateByPrimaryKey(tOrder);
                TOrdersetting tordate = tOrdersettingService.tordate(tOrder.getOrderdate());
                System.out.println(tordate);
                if(tordate==null){
                    TOrdersetting tOrdersetting=new TOrdersetting();
                    tOrdersetting.setOrderdate(tOrder.getOrderdate());
                    tOrdersetting.setNumber(99);
                    tOrdersetting.setReservations(1);
                    tOrdersetting.setCreatetime(date);
                    tOrdersetting.setUpdatetime(date);
                    int insert = tOrdersettingService.insert(tOrdersetting);
                }
                if(tordate!=null){
                    TOrdersetting tOrdersetting=new TOrdersetting();
                    tOrdersetting.setId(tordate.getId());
                    tOrdersetting.setOrderdate(tordate.getOrderdate());
                    tOrdersetting.setReservations(tordate.getReservations()+1);
                    tOrdersetting.setNumber(tordate.getNumber()-1);
                    tOrdersetting.setCreatetime(tordate.getCreatetime());
                    tOrdersetting.setUpdatetime(tordate.getUpdatetime());
                    int i1 = tOrdersettingService.updateByPrimaryKey(tOrdersetting);
                }
            }
        }
        if(tOrder.getOrderstatus().equals("取消预约")){
            tOrder.setOrderstatus("未到诊");
            i = tOrderService.updateByPrimaryKey(tOrder);
            System.out.println(111);
            TOrdersetting tordate = tOrdersettingService.tordate(tOrder.getOrderdate());
            if(tordate==null){
                TOrdersetting tOrdersetting=new TOrdersetting();
                tOrdersetting.setOrderdate(tOrder.getOrderdate());
                tOrdersetting.setNumber(100);
                tOrdersetting.setReservations(0);
                tOrdersetting.setCreatetime(date);
                tOrdersetting.setUpdatetime(date);
            }
            if(tordate!=null){
                TOrdersetting tOrdersetting=new TOrdersetting();
                tOrdersetting.setId(tordate.getId());
                tOrdersetting.setOrderdate(tordate.getOrderdate());
                tOrdersetting.setReservations(tordate.getReservations()-1);
                tOrdersetting.setNumber(tordate.getNumber()+1);
                tOrdersetting.setCreatetime(tordate.getCreatetime());
                tOrdersetting.setUpdatetime(tordate.getUpdatetime());
                int i1 = tOrdersettingService.updateByPrimaryKey(tOrdersetting);
                System.out.println(i1);
            }
        }
        return i;
    }
    //预约设置
    @GetMapping("/tOrdersettingList")
    public List<TOrdersetting> tOrdersettingList(){
        List<TOrdersetting> tOrdersettings = tOrdersettingService.tordersetList();
        return  tOrdersettings;
    }
    //会员统计
    @PostMapping("/{nf}")
    public List<DateShi> tatib(@PathVariable("nf") String nf){
        String s1=null;
        String s2=null;
        String s3=null;
        List<DateShi> dateShiList=new ArrayList<>();
        for(int i=1;i<=12;i++){
            if(i<10){
                s1="0"+i;
            }
            s2=nf+"-"+s1+"%";
            s3=nf+"-"+s1;
            String zctime = tMemberRegisterService.zctime(s2);
            if(zctime==null){
                zctime="0";
            }
            String dltime = tMemberDldateService.dltime(s2);
            if(dltime==null){
                dltime="0";
            }
            String yytime = tOrdersettingService.yytime(s2);
            if(yytime==null){
                yytime="0";
            }
            DateShi dateShi=new DateShi();
            dateShi.setDates(s3);
            dateShi.setYytime(yytime);
            dateShi.setZctime(zctime);
            dateShi.setDltime(dltime);
            dateShiList.add(dateShi);
        }
        return dateShiList;
    }
    //体检报告
    @PostMapping("/smm")
    public List<TBodyCx> smm(@RequestBody Map<String, String> map){
        TMemberExamination tMemberExamination = tMemberExaminationService.tmemberExid(Integer.parseInt(map.get("id")));
        System.out.println(tMemberExamination);
        List<TBodyCk> tbodyckid = tBodyCkService.tbodyckid(tMemberExamination.getId());
        List<TBodyCx> list=new ArrayList<>();
        for (TBodyCk tBodyCk : tbodyckid) {
            TCheckitem tCheckitem = tCheckitemService.selectByPrimaryKey(tBodyCk.getTCheckitemId());
            TBodyCx tBodyCx=new TBodyCx();
            tBodyCx.setName(tCheckitem.getName());
            tBodyCx.setJg(tBodyCk.getJg());
            tBodyCx.setMaxck(tCheckitem.getMaxck());
            tBodyCx.setMinck(tCheckitem.getMinck());
            list.add(tBodyCx);
        }
       return list;
    }
}
